/**
 * 公司成交情况
 * - 使用自定义封装组件，简化代码，统一管理。
 * - 2025-07-12 12:10:21 tzj
 * - 原文件备份：./index_backup.js
 * */

import BaseComponent from "../../components/BaseComponent";
import {mrhzApi} from "../../services/basic";

// 要显示的form表单
const formItemList =
    [
        "companies",
        "serviceWx2",
        {
            name: 'personZw',
            keyword: 'dg',
            label: '导购',
            zw: '3'
        },
        {
            name: "dateRange",
            label:"留资日期",
            start: "qsrq",
            end: "jsrq"
        },
    ];
// 显示的按钮
const buttons = ["query", "export",];
// 表格列信息
const columns = [
    {
        title: '分公司名称',
        dataIndex: 'gsmc',
        width: "40%",
        sumName: "合计"
    },
    {
        title: '总留资',
        dataIndex: 'zlz',
        sorter: true
    },
    {
        title: '总金额',
        dataIndex: 'zje',
    },
    {
        title: '总单价',
        dataIndex: 'zdj',
    },
]

class pageGscjtj extends BaseComponent {
    constructor(props) {
        super(props);
        this.state = {
            ...this.state,
            formItemList,
            buttons,
            // columns,
            filename: "鹏成车邦分公司留资情况报表",
            tableTitle: true,
            queryAll: true,
            sumRow: true,
            expandable: {
                expandRowByClick: true,
                rowExpandable: (record) => !!record.children, // 可选：控制哪些行可以展开
            }
        }
        this.columns = columns;
        this.queryApi = mrhzApi.getPagegs;
        this.hasLzModal = true;
    }

    dataProcess(arr) {
        const map = new Map();

        arr.forEach(item => {
            const obj = {
                // gsmc:item.gsmc,
                // zhmc:item.zhmc,
                gsmc: item.zhmc,
                jgmc: item.jgmc,
                zlz: item.zlz,
                yybm: item.yybm,
                zhbh: item.zhbh,
            }
            if (map.has(item.gsmc)) {
                const existing = map.get(item.gsmc);
                existing.children.push(obj);
                existing.zlz += Number(item.zlz);
            } else {
                map.set(item.gsmc, {
                    gsmc: item.gsmc,
                    children: [obj],
                    zlz: Number(item.zlz)
                });
            }
        });

        // 转换Map为数组并添加层级key
        const list = Array.from(map.values()).map((parent, parentIndex) => {
            const parentKey = (parentIndex + 1).toString();

            // 为子项添加层级key
            const childrenWithKeys = parent.children.map((child, childIndex) => ({
                ...child,
                key: `${parentKey}-${childIndex + 1}`
            }));

            return {
                ...parent,
                key: parentKey,
                children: childrenWithKeys
            };
        });

        list.sort((a, b) => {
            return b.zlz - a.zlz;
        })
        return list;
    }

    onRowDoubleClick = (record) => {
        if (record.children) return;
        const params = {
            ...this.form.dataForm,
            yybm: record.yybm,
            sfzh: record.zhbh,
        }
        params.zzrq = params.jsrq;
        delete params.jsrq;
        // gsmc:"鹏成车邦总部"
        // yybm:"20000000000000001"
        // zhbh:"98551941147"
        // zhmc:"鹏成车邦总部·360全景"

        // const config = [
        //     {
        //         key: "gsmc",
        //         val: record.gsmc,
        //         label: "公司名称",
        //     },
        //     {
        //         key: "zhmc",
        //         val: record.zhmc,
        //         label: "来源账号",
        //     },]
        // 日期范围
        this.lzModalRef.open(params, {});
    }
}

export default pageGscjtj;
